/******************************************************************
 * 个人博客：https://blog.csdn.net/2302_80277720
 * 嵌入式Linux学习交流群：1005210698
 * 欢迎各位大佬和萌新来加入交流学习
 * Change Logs:
 * Date           Author       Notes
 * 2025-08-24     喝呜昂黄    first version
 * 2025-08-30     喝呜昂黄    修改页翻转，添加日志功能
 ******************************************************************/

#include <stdio.h>
#include <string.h>

#include "common.h"
#include "video_manager.h"

static video_opr_t *video_head;

int register_video(video_opr_t *video_opr) {
    video_opr_t *tmp;

    if (!video_head) {
        video_head = video_opr;
    } else {
        tmp = video_head;
        while (tmp->next) {
            tmp = tmp->next;
        }
        tmp->next = video_opr;
    }
    video_opr->next = NULL;

    MEDIA_LOG_INFO("All the video module register success");

    return 0;
}

image_format_t get_img_format(int fmt) {
    switch (fmt) {
        case V4L2_PIX_FMT_RGB24:
            return IMAGE_FORMAT_RGB888;
        case V4L2_PIX_FMT_RGB32:
            return IMAGE_FORMAT_RGBA8888;
        case V4L2_PIX_FMT_NV12:
            return IMAGE_FORMAT_YUV420SP_NV12;
        case V4L2_PIX_FMT_NV21:
            return IMAGE_FORMAT_YUV420SP_NV21;
        case V4L2_PIX_FMT_MJPEG:
            return IMAGE_FORMAT_YUV420SP_NV21;
        default:
            return -1;
    }
}

video_opr_t *get_video(const char *pcName) {
    video_opr_t *tmp = video_head;

    while (tmp) {
        if (strcmp(tmp->name, pcName) == 0)
            return tmp;

        tmp = tmp->next;
    }

    return NULL;
}

int all_video_init(void) {
    int ret = 0;

    video_opr_t *tmp = video_head;

    while (tmp) {
        ret &= tmp->dev_init(video_head->video);
        tmp = tmp->next;
    }

    return ret;
}

int all_video_start(void) {
    int ret = 0;

    video_opr_t *tmp = video_head;

    while (tmp) {
        ret &= tmp->dev_start(video_head->video);
        tmp = tmp->next;
    }

    return ret;
}
